package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.hibernate.criterion.Order;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import com.aplikata.bo.Node;
import com.aplikata.bo.Parameter;
import com.aplikata.bo.Role;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.dataModel.NodeDataModel;
import com.aplikata.dataModel.ParameterDataModel;
import com.aplikata.dataModel.RoleDataModel;
import com.aplikata.service.RoleService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

/**
 * @ClassName: RoleBean
 * @Description: TODO
 * @author Huyun
 * @date 27. 2. 2012. 08:47:14
 */
@ManagedBean(name = "evokRoleBean")
@RequestScoped
public class EvokRoleBean extends UIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{roleService}")
	private RoleService roleService;

	private RoleDataModel roleModel;
	private List<Role> roleList;

	private List<Parameter> nodeTypeList;
	private ParameterDataModel paModel;

	private Node[] selectedNodes;
	private List<Node> nodeList;
	private NodeDataModel nodeModel;

	private Role role;
	private Parameter nodeType;

	public EvokRoleBean() {
		role = new Role();
	}

	@SuppressWarnings("unchecked")
	public void selectNodeType(SelectEvent event) {
		nodeType = (Parameter) event.getObject();
		nodeList = (List<Node>) roleService.findByNamedQuery("Node.findByType", new Object[] { nodeType.getValue() });
		List<Node> nodes = (List<Node>) roleService.findByNamedQuery("Node.findRoleNodes", new Object[] { nodeType.getValue(),
				role.getId() });
		
		selectedNodes = nodes.toArray(new Node[0]);
		getNodeModel().setWrappedData(nodeList);
	}

	public void selectRole(SelectEvent event) {
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("wdgNodeTypeList.unselectAllRows()");
		getNodeModel().setWrappedData(null);
	}

	public void submit(){
		try {
			roleService.updateRoleDynamic(role, nodeType.getValue(), selectedNodes);
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS);
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> getRoleList() {
		if (roleList == null) {
			roleList = (List<Role>) roleService.findAll(Role.class, new Order[] { Order.asc(Role.NAME) });
		}

		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public RoleDataModel getRoleModel() {
		if (roleModel == null) {
			roleModel = new RoleDataModel(roleService, getRoleList());
		}
		return roleModel;
	}

	public void setRoleModel(RoleDataModel roleModel) {
		this.roleModel = roleModel;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Parameter> getNodeTypeList() {
		if (nodeTypeList == null) {
			nodeTypeList = FacesUtil.getSelectItemList("NodeType");
		}
		return nodeTypeList;
	}

	public void setNodeTypeList(List<Parameter> nodeTypeList) {
		this.nodeTypeList = nodeTypeList;
	}

	public ParameterDataModel getPaModel() {
		if (paModel == null) {
			paModel = new ParameterDataModel(roleService, this.getNodeTypeList());
		}
		return paModel;
	}

	public void setPaModel(ParameterDataModel paModel) {
		this.paModel = paModel;
	}

	public List<Node> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<Node> nodeList) {
		this.nodeList = nodeList;
	}

	public Node[] getSelectedNodes() {
		return selectedNodes;
	}

	public void setSelectedNodes(Node[] selectedNodes) {
		this.selectedNodes = selectedNodes;
	}

	public Parameter getNodeType() {
		return nodeType;
	}

	public void setNodeType(Parameter nodeType) {
		this.nodeType = nodeType;
	}

	public NodeDataModel getNodeModel() {
		if (nodeModel == null) {
			nodeModel = new NodeDataModel(roleService, this.getNodeList());
		}
		return nodeModel;
	}

	public void setNodeModel(NodeDataModel nodeModel) {
		this.nodeModel = nodeModel;
	}
	
	public String getCalendarUrl(){
		String urlString = FacesUtil.getUserFromSession().getRole().getName();
		if(urlString.startsWith("doc-")){
			return "/pages/aplikata/evok/doctor/doctor_schedule.xhtml";
		}
		if(urlString.startsWith("patient")){
			return "/pages/aplikata/evok/patient/patient_schedule.xhtml";
		}
		return "/pages/aplikata/oa/userEvent.xhtml";
	}
	
	public boolean isRoleDoctor(){
		String urlString = FacesUtil.getUserFromSession().getRole().getName();
		if(urlString.startsWith("doc-")){
			return true;
		}
		return false;
	}
}
